/*******************************************************************************
  * @file                   main.c
  * @Author:                MQjehovah                 mail:MQjehovah@hotmail.com
  * @version                1.0.0
  * @date                   2017.8.24
  * @brief                  
  ******************************************************************************
  * @attention
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "USART.h"
#include "shell.h"
#include "delay.h"
#include "ring_buffer.h"
#include "stdio.h"
/* Definition ----------------------------------------------------------------*/

/* Functions -----------------------------------------------------------------*/
/*******************************************************************************
  * @brief  时钟配置            
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void RCC_Config()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);  //使能GPIOA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1时钟
}

/*******************************************************************************
  * @brief  中断优先级配置            
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void NVIC_Config(void) //配置中断优先级
{
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级 2
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;        //响应优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //IRQ 通道使能
    NVIC_Init(&NVIC_InitStructure);                           //根据指定的参数初始化 VIC 寄存器、
}

/*******************************************************************************
  * @brief  主函数              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
int main(void)
{
    RCC_Config();
    NVIC_Config();
    SysTick_init();
    USART_Config(USART1, 115200);
    USART_SendStr(USART1, "\r\n");
    USART_SendStr(USART1, " _                 _   _                 _\r\n");
    USART_SendStr(USART1, "| |__   ___   ___ | |_| | ___   __ _  __| | ___ _ __\r\n");
    USART_SendStr(USART1, "| '_ \\ / _ \\ / _ \\| __| |/ _ \\ / _` |/ _` |/ _ \\ '__|\r\n");
    USART_SendStr(USART1, "| |_) | (_) | (_) | |_| | (_) | (_| | (_| |  __/ |\r\n");
    USART_SendStr(USART1, "|_.__/ \\___/ \\___/ \\__|_|\\___/ \\__,_|\\__,_|\\___|_|\r\n");
    if (Waiting_Key(5))
    {
        shell_loop();
    }
    JUMP_TO_USERAPP(); //模拟跳转到用户程序
}
/*******************************************************************************
  * @brief  等待用户按下一个键              
  * @param  timeout：超时时间              
  * @retval 0：未按下
            1：按下
  * @Note   None              
*******************************************************************************/
u8 Waiting_Key(u8 timeout)
{
    while (timeout--)
    {
        USART_SendStr(USART1, "\rpress any key into bootloader  ");
        USART_SendChar(USART1, timeout + 0x30);
        u32 start = sys_Time;
        while (sys_Time - start < 1000)
        {
            if (rx_buffer.lenth > 0)
            {
                USART_SendStr(USART1, "\rJUMPING INTO BOOTLOADER......\r\n");
                RingBuf_flush();
                return 1;
            }
        }
    }
    USART_SendStr(USART1, "\rJUMPING INTO APPLICATION......\r\n");
    return 0;
}
/*******************************************************************************
  * @brief  跳转到用户程序              
  * @param  None    
  * @retval None
  * @Note   None              
*******************************************************************************/
void JUMP_TO_USERAPP(void)
{
    while (1)
        ;
}
/*********************************END OF FILE**********************************/
